package com.szholly.pro.watergas_server.ps.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.szholly.data.general.*;
import com.szholly.plug.form.excel.utils.StringUtil;
import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.spring.SpringBeanFactory;
import org.springframework.stereotype.Service;

@Service
public class PeroidMaintainanceService {

	/**
	 * 获取定期维护列表
	 * @param params
	 * @param pageInfo
	 * @return
	 * @throws Exception
	 */
	public List<RowBase> getPeroidMaintainanceList(Map<String,String> params, PageInfo pageInfo) throws Exception {
		List<GlobalParameter> listParam = new ArrayList<GlobalParameter>();
		 String sql=
	        		"select * from (select t.*,\n" +
	        				"    su.c_userealname,\n" + 
	        				"    og.group_name,\n" + 
	        				"    og.GROUP_LEADER,\n" + 
	        				"    ms.name,\n" + 
	        				"    ms.station_category\n" + 
	        				"   from (\n" + 
	        				"    select oir.*,ot.station_id,ot.operation_group_id,ot.start_time,ot.finish_time,ot.end_time,ot.finish_by\n" + 
	        				"     from (\n" + 
	        				"       select o_r.*,t1.s_domaincaption as item_name,t2.s_domaincaption as pitem_name,t3.s_domaincaption as ppitem_name\n" + 
	        				"        from operation_record_maintainance o_r\n" + 
	        				"           left join (select s_domaincode,s_domaincaption,s_id,s_pid from data_defulatdomain where s_domainname=@s_domainname) t1 on o_r.item=t1.s_domaincode\n" + 
	        				"           left join (select s_domaincode,s_domaincaption,s_id,s_pid from data_defulatdomain where s_domainname=@s_domainname) t2 on t1.s_pid=t2.s_id\n" + 
	        				"           left join (select s_domaincode,s_domaincaption,s_id,s_pid from data_defulatdomain where s_domainname=@s_domainname) t3 on t2.s_pid=t3.s_id\n" + 
	        				"        ) oir\n" + 
	        				"    left join operation_task ot on oir.task_code=ot.task_code\n" + 
	        				"   ) t\n" + 
	        				"   left join (\n" + 
	        				"      select og1.*,tt1.c_userealname as GROUP_LEADER from OC_GROUP og1\n" + 
	        				"        left join (\n" + 
	        				"          select distinct o1.group_id,o1.group_head,su1.c_userealname\n" + 
	        				"          from OC_GROUP_ASSO o1\n" + 
	        				"            left join safe_user su1 on o1.user_id=su1.s_userid\n" + 
	        				"            where o1.group_head='1'\n" + 
	        				"        ) tt1 on og1.guid = tt1.group_id\n" + 
	        				"   ) og on t.operation_group_id = og.guid\n" + 
	        				"   left join safe_user su on t.finish_by = su.s_userid\n" + 
	        				"   left join BC_SITE_INFO ms on t.station_id=ms.code ) fr where 1=1 ";
		if(params.containsKey("task_code")){
			sql += " and fr.task_code = @task_code";
			listParam.add(new GlobalParameter("task_code", params.get("task_code")));
		}
			if (params.containsKey("flow_type")) {
				sql += " and fr.f_type = @flow_type";
				listParam.add(new GlobalParameter("flow_type", params.get("flow_type")));
			}
			if (params.containsKey("water_type")) {
				sql += " and fr.station_category = @water_type";
				listParam.add(new GlobalParameter("water_type", params.get("water_type")));
			}
			if (params.containsKey("station_id")) {
				sql += " and fr.station_id = @station_id";
				listParam.add(new GlobalParameter("station_id", params.get("station_id")));
			}
			if(params.containsKey("start")){
				sql += " and fr.start_time >= to_date('" + params.get("start") + " 00:00:00', 'YYYY-MM-DD HH24:MI:SS')";
			}
			if(params.containsKey("end")){
				sql += " and fr.start_time <= to_date('" + params.get("end") + " 23:59:59', 'YYYY-MM-DD HH24:MI:SS')";
			}
		
		
		if(params.containsKey("pitem_name")){
			String str=params.get("s_domainname");
           if(str.equals("运维项")){
        	   sql += " and fr.ppitem_name=@pitem_name";
   			listParam.add(new GlobalParameter("pitem_name", params.get("pitem_name")));
           }
           if(str.equals("供水运维项")){
        	   sql += " and fr.pitem_name=@pitem_name";
   			listParam.add(new GlobalParameter("pitem_name", params.get("pitem_name")));
           }
		}
		
		if(params.containsKey("s_domainname")){
			listParam.add(new GlobalParameter("s_domainname", params.get("s_domainname")));
		}
		
		return DataUtil.getPageRow(sql, listParam, null, pageInfo);
	}
	
	/**
	 * 获取定期维护任务数据记录列表
	 * 
	 * @param params
	 * @return
	 */
	public List<RowBase> getPeriodicalMaintainanceDataList(Map<String, String> params) {
		if (params == null)
			return null;

		String strSTATION_ID = params.get("STATION_ID");
		String strJOB_ID = params.get("JOB_ID");
		String strSTART_TIME = params.get("START_TIME");
		String strEND_TIME = params.get("END_TIME");
		if (StringUtil.IsNullOrEmpty(strSTATION_ID) || StringUtil.IsNullOrEmpty(strJOB_ID))
			return null;

		String strSQL = " select t.TASK_CODE, t.TASK_STATUS, t.START_TIME as TASK_START_TIME, t.END_TIME as TASK_END_TIME, t.FINISH_TIME as TASK_FINISH_TIME,  \n"
				+ " t1.*, \n" + " t2.NAME as STATION_NAME, \n" + " t3.GROUP_NAME as GROUP_NAME, \n"
				+ " t4.C_USEREALNAME as USER_NAME \n" + " from OP_TASK t \n"
				+ " inner join OP_MAIN_RECORD t1 ON t1.TASK_CODE = t.TASK_CODE \n" // dif
				+ " left join BC_SITE_INFO t2 on t2.EXP_NO = t.STATION_ID \n"
				+ " left join OC_GROUP t3 on t3.GUID = t.OPERATION_GROUP_ID \n"
				+ " left join SAFE_USER t4 on t4.S_USERID = t.FINISH_BY \n" + " where t.STATION_ID = '" + strSTATION_ID
				+ "' \n" + " AND t.JOB_ID = '" + strJOB_ID + "' \n"
				// + " AND t1.PARAM = '" + strPARAM_ID + "' \n" //dif
				+ " AND t.START_TIME >= to_date('" + strSTART_TIME + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') \n"
				+ " AND t.START_TIME <= to_date('" + strEND_TIME + " 00:00:00', 'yyyy-mm-dd hh24:mi:ss') \n"
				+ " order by t.START_TIME desc ";

		ITableFactory tableFactory = (ITableFactory) SpringBeanFactory.getBean("TableFactory");
		ITable pTable = tableFactory.openTable("TASKQUERYDIC");
		List<RowBase> rcdList = DataUtil.getSelectRows(strSQL, null, pTable);
		return rcdList;
	}
}
